<template>
    <div class="template" style="background-color: #9E3F78;">
        <div class="nav" style="position: relative;">
            <img :src="back" style="width: 28px;height: 28px;margin-left: 15px;" @click="router.back()"/>
            <div style="position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);">Store</div>
            <div class="lookNum">
                <img :src="coins" style="height: 100%;margin-right: 4px;"/>
                {{ coinsNum }}
            </div>
        </div>
        <img :src="storeTop" style="width: 90vw;height: 30vh ;margin-top: 1px;"/>
        <div v-for="(item, index) in buyList" :key="index" class="buyItem" @click="buyItem(index)">
            <img :src="coins" style="height: 60%;"/>
            <div class="buyInfo">
                <div>×{{ item.num }} coins</div>
                <div class="buy">${{ item.money }}</div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { showToast, showLoadingToast, closeToast } from 'vant';
import back from '../assets/back.png'
import coins from '../assets/coins.png'
import storeTop from '../assets/storeTop.png'
let buyList = [{num: 20, money: 1.99 },{num: 50, money: 2.99 },{num: 100, money: 4.99 },{num: 200, money: 9.99 }]
import { useRouter } from 'vue-router';
import { onMounted,ref } from 'vue';
import {Base64} from 'js-base64'

const router = useRouter()
const coinsNum = ref(0)
onMounted(() => {
    coinsNum.value = localStorage.getItem('PuzzleEditCoinsNum') ? Number(localStorage.getItem('PuzzleEditCoinsNum')) : 0
})
const buyItem = (index) => {
    showLoadingToast({
      duration: 0,
      message: 'Loading...',
      forbidClick: true,
    });
     window['iap'] = (data) => {
            let res;
            try {
                const decodedString = Base64.decode(data);
                res = JSON.parse(decodedString);
            } catch (error) {
                res = data;
            }
            if (res !== null && typeof res === 'object') {
                let num = res.productIdentifier.replace(/\D/g, '');
    			coinsNum.value = coinsNum.value + Number(num)
                localStorage.setItem('PuzzleEditCoinsNum', coinsNum.value)
    			closeToast()
            } else {
                closeToast()
                showToast('Payment failed!');
            }
            return 0;
    }
    window['send'](`terms://buy?product=com.moreuse.edtings.${buyList[index].num}s&num=1`);
}
</script>
<style scoped>
@import '../font/index.css';
.lookNum {
    height: 24px;
    min-width: 50px;
    background: #FFFFFF;
    border-radius: 12px 12px 12px 12px;
    margin-right: 15px;
    font-weight: 400;
    font-size: 12px;
    color: #9E3F78;
    display: flex;
    align-items: center;
    padding-right: 5px;
    box-sizing: border-box;
}
.buyItem {
    width: 345px;
    height: 74px;
    background: #FFFFFF;
    border-radius: 20px 20px 20px 20px;
    margin-top: 18px;
    display: flex;
    align-items: center;
    padding: 0 10px;
    box-sizing: border-box;
}
.buyInfo {
    margin-left: 15px;
    flex: 1;
    display: flex;
    justify-content: space-between;
    font-family: TitanOne-Regular;
    font-weight: 400;
    font-size: 24px;
    color: #000000;
}
.buy {
    width: 75px;
height: 35px;
background: #9E3F78;
border-radius: 18px 18px 18px 18px;
font-weight: 500;
font-size: 14px;
color: #FFFFFF;
display: flex;
justify-content: center;
align-items: center;
}
</style>